<?php
class ProjectsController extends AppController{

	var $uses = array( 'Project', 'CategoriesProject' );

	function index(){
		$listProjects =  $this->Project->findAllByPersonId( 1 );
		$listAlgorithms = $this->Project->Algorithm->find( 'list', array( 'fields' => array( 'id', 'name', 'description' ) ) );
		
		$this->set( compact( 'listProjects', 'listAlgorithms' ) );
	}
	
	
	function add(){
		if( $this->data ){
			$this->data['Project']['person_id'] = 1;
			$this->Project->save( $this->data );
			$this->Session->setFlash( 'El proyecto ha sido salvado con exito.' );
			$this->redirect( $this->referer() );
		}
	}
	
	function addCategory(){
		if( $this->data ){		
			$this->data['Category']['person_id'] = 1; // Temporal		
			
			if( $this->Project->Category->save( $this->data ) ){
				$this->Session->setFlash( 'La carpeta ha sido creada con exito.' );
			}else{
				$this->Session->setFlash( 'Ha ocurrido un error al crear la carpeta.' );
			}
		}
		
		$this->redirect( $this->referer() );
	}
	
	
	function view( $intProjectId ){
		$this->Project-> id = $intProjectId;
		$listProject = $this->Project->read();
	
		$this->Session->write( 'prevProject', $listProject );

	
		$this->set( compact( 'listProject' ) );
	}
	
	
	function delete( $intProjectId ){
		$this->Project->delete( $intProjectId, true );
		$this->Session->setFlash( 'El proyecto ha sido eliminado, junto con todas las carpetas y tareas relacionadas a este.' );
		$this->redirect( $this->referer() );
	}
	
	
	function deleteCategory( $intCategoryId ){
		$this->Project->Category->delete( $intCategoryId, true );
		$this->Session->setFlash( 'La carpeta ha sido eliminada, junto con todas las tareas relacionadas a este.' );
		$this->redirect( $this->referer() );
	}
	
}
 ?>